package com.dq.service.sys.role.impl;

import com.dq.context.Constant;
import com.dq.exception.customException.AdminException;
import com.dq.domain.sys.role.SysRole;
import com.dq.domain.sys.role.SysRoleSection;
import com.dq.domain.sys.role.SysRoleMenu;
import com.dq.domain.sys.role.SysUserRole;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.repository.BaseRepositories;
import com.dq.service.sys.role.SysRoleService;
import com.dq.service.sys.user.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author  gewenzhen
 */
@Service
public class SysRoleServiceImpl extends BaseRepositories implements SysRoleService {
    @Autowired
    private SysUserService sysUserService;

    @Override
    public List<Long> findAllDeptIdByRole(SysRole sysRole) {
        return dao.find("select distinct(section.id) from SysRoleSection where sysRole=?0 ", new Object[]{sysRole});
    }

    @Override
    public List<Long> findAllMenuIdByRole(SysRole role) {
        return dao.find("select distinct(sysMenu.id) from SysRoleMenu where sysRole=?0 ", new Object[]{role});
    }

    /**
     * 删除角色， 同时删除角色关联中间表（菜单/用户/部门）
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBath(SysRole sysRole) {
        sysRoleRepository.delete(sysRole);

        List<SysRoleMenu> sysRoleMenus = sysRoleMenuRepository.findBySysRole(sysRole);
        sysRoleMenuRepository.deleteAll(sysRoleMenus);

        List<SysUserRole> sysUserRoles = sysUserRoleRepository.findBySysRole(sysRole);
        sysUserRoleRepository.deleteAll(sysUserRoles);

        List<SysRoleSection> sysRoleDepts = sysRoleSectionRepository.findByDeletedFalseAndSysRole(sysRole);
        sysRoleSectionRepository.deleteAll(sysRoleDepts);
    }


    @Override
    @Transactional
    public SysRole save(SysRole sysRole)  throws AdminException {

        sysRole = objectSaveBase(sysRole);

        dao.save(sysRole);

        return sysRole;
    }

    /**
     * 添加对象基本属性配置
     */
    @Override
    public SysRole objectSaveBase(SysRole sysRole) throws AdminException {
        if(sysRole.getStatus() == null) {
            sysRole.setStatus(SysRole.Status.on);
        }

        sysRole.setVersion(Constant.VERSION_VALUE);
        sysRole.setDeleted(false);
        sysRole.setCreateTime(System.currentTimeMillis());
        sysRole.setLastModifyTime(System.currentTimeMillis());

        SysUser user = sysUserService.getUser();
        sysRole.setCreateUser(user);
        sysRole.setUpdateUser(user);

        return sysRole;
    }

    /**
     *修改
     */
    @Override
    public SysRole update(SysRole sysRole) throws AdminException {
        sysRole.setLastModifyTime(System.currentTimeMillis());

        SysUser user = sysUserService.getUser();
        if(user != null) {
            sysRole.setUpdateUser(user);
        }

        sysRoleRepository.save(sysRole);

        return sysRole;
    }


    /**
     * 用户角色中间表
     * 对象初始化
     */
    @Override
    public SysUserRole initObjectUserRole(SysUserRole sysUserRole) throws AdminException {
       if(sysUserRole.getSysRole() == null) {
           throw new AdminException("未关联角色");
       }
       if(sysUserRole.getSysUser() == null) {
           throw new AdminException("未关联用户");
       }

        sysUserRole.setVersion(Constant.VERSION_VALUE);
        sysUserRole.setDeleted(false);
        sysUserRole.setCreateTime(System.currentTimeMillis());
        sysUserRole.setLastModifyTime(System.currentTimeMillis());

        SysUser user = sysUserService.getUser();
        sysUserRole.setCreateUser(user);
        sysUserRole.setUpdateUser(user);

        return sysUserRole;
    }

    /**
     * 判断一个 用户 是否有管理者角色 或者 ID = 1
     * @param user 用户
     * @return boolean
     */
    @Override
    public boolean isAdminRole(SysUser user) {
        boolean t = isRole(user, 0);

        return user.getId().toString().equals(String.valueOf(Constant.ADMIN_ID)) || t;
    }
    /**
     * 判断一个 用户 是否有 财务 角色
     * @param user 用户
     * @return boolean
     */
    @Override
    public boolean isFinanceRole(SysUser user) {
        return isRole(user, 2);
    }
    /**
     * 判断一个 用户 是否有 仓库 角色
     * @param user 用户
     * @return boolean
     */
    @Override
    public boolean isDepotRole(SysUser user) {
        return isRole(user, 1);
    }

    /**
     * 判断一个 用户 是否有 之账号 角色
     */
    @Override
    public boolean isBossSonRole(SysUser user) {
        return isRole(user, 3);
    }

    /**
     * 判断一个 用户 是否有 对应 角色
     */
    @Override
    public boolean isRole(SysUser user, int i) {
        if (user == null) {
            throw new AdminException("user 为空");
        }
        List<SysUserRole> userRoleList = sysUserRoleRepository.findByDeletedIsFalseAndSysUser(user);
        boolean t = false;
        for (SysUserRole sysUserRole : userRoleList) {
            if (sysUserRole.getSysRole().getCode().equals(Constant.ROLE_CODE_LIST.get(i))) {
                t = true;
                break;
            }
        }
        return t;
    }
}
